/** Block represents several page **/

package qp.utils;

import java.util.Vector;
import java.io.Serializable;

public class Block  implements Serializable{

	public int maxNumBatchInBlock;       
    public int existTupleInBlock; 
    Vector<Batch> pageBlock; 
	
	public Block (int numberBatch)
	{
		maxNumBatchInBlock = numberBatch;
		pageBlock = new Vector(maxNumBatchInBlock);
		existTupleInBlock = 0;
	}

	public Batch remove(int index) {
		existTupleInBlock=existTupleInBlock-pageBlock.elementAt(index).size();	
		return pageBlock.remove(index);
	}
		
	public void add (int index, Batch page)
	{
		pageBlock.add(index, page);
		existTupleInBlock+=page.size();
	}

	public void  emptyBlock()
	{
		existTupleInBlock=0;
	}

	public boolean isEmpty()
	{
		return (existTupleInBlock==0);
	}
	
	public void clear() 
	{
		pageBlock.clear();
		existTupleInBlock=0;
	}

	public int findMinFromSortedBatch(int index) 
	{
		int minimal=0;
		int compareFlag=0;
		for(int i=0;i<pageBlock.size();i++) {
			if(pageBlock.elementAt(i).size()==0) {
				continue;
			}
			else{
				try {
					compareFlag=Tuple.compareTuples(pageBlock.elementAt(minimal).elementAt(0), pageBlock.elementAt(i).elementAt(0), index);
					if(compareFlag>0) {
						minimal=i;
					}
				}
				catch (ArrayIndexOutOfBoundsException e) {
					minimal=i;
					continue;
				}
				catch (Exception e) {
				}
			}
		}
		return minimal;
	}
	
	public int findMaxFromSortedBatch(int index) 
	{
		int max=0;
		int compareFlag=0;
		for(int i=0;i<pageBlock.size();i++) {
			if(pageBlock.elementAt(i).size()==0) {
				continue;
			}
			else{
				try {
					compareFlag=Tuple.compareTuples(pageBlock.elementAt(max).elementAt(0), pageBlock.elementAt(i).elementAt(0), index);
					if(compareFlag<0) {
						max=i;
					}
				}
				catch (ArrayIndexOutOfBoundsException e) {
					max=i;
					continue;
				}
				catch (Exception e) {
				}
			}
		}
		return max;
	}
	
	public int findApproFromSortedBatch(Vector<Integer> index, Vector<String> AscOrDesc) 
	{
		int pick=0;
		int compareFlag=0;
		for(int i=0;i<pageBlock.size();i++) {
			if(pageBlock.elementAt(i).size()==0) {
				continue;
			}
			else{
				try {
					/*
					compareFlag=Tuple.compareTuples(pageBlock.elementAt(minimal).elementAt(0), pageBlock.elementAt(i).elementAt(0), index);
					if(compareFlag>0) {
						minimal=i;
					}*/
					boolean find=false;
					for(int f=0;f<AscOrDesc.size()&&(!find);f++)
					{
						compareFlag=Tuple.compareTuples(pageBlock.elementAt(pick).elementAt(0), pageBlock.elementAt(i).elementAt(0),index.elementAt(f));
						if(AscOrDesc.elementAt(f).equalsIgnoreCase("asc"))
						{
							if(compareFlag<0)//J > pick
							{
								find=true;
							}//no action
							else if(compareFlag>0)//j<pick
							{
								pick=i;
							    find=true;
							}
							//else compareFlag==0
						}
						if(AscOrDesc.elementAt(f).equalsIgnoreCase("desc"))
						{
							if(compareFlag<0)//J > pick
							{
								pick=i;
								find=true;
							}//no action
							else if(compareFlag>0)//j<pick
							{
							    find=true;
							}
							//else compareFlag==0
						}			
					}
					
					
				}
				catch (ArrayIndexOutOfBoundsException e) {
					pick=i;
					continue;
				}
				catch (Exception e) {
				}
			}
		}
		return pick;
	}
	
	
	public Batch batchAt(int index) 
	{
		return pageBlock.elementAt(index);
	}

	public int size() 
	{
		return pageBlock.size();
	}
	
	public int sizeTuple()
	{
		return existTupleInBlock;
	}

	public Tuple  elementAt(int index)
	{
		int tmpsize=((Batch) pageBlock.elementAt(0)).size();
		return (Tuple)((Batch)pageBlock.elementAt(index/tmpsize)).elementAt(index%tmpsize);		
	}
}